home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / Samples / Moofwars 1.02 / MoofWars Sprocket / •Source / TShipSprite.cp < prev    next >
Encoding:
Text File  |  1997-11-05  |  4.6 KB  |  164 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. TShipSprite.cp
  4.  
  5. This is the class used to represent the friendly ship in the game.  The enemy ship
  6. is only slightly different, so there might be some opportunity to combine the TEnemySprite
  7. and TShipSprite classes.
  8.  
  9. Author: Timothy Carroll
  10. Apple Developer Technical Support
  11. timc@apple.com
  12.  
  13. Modification History: 
  14.  
  15. 1/23/97     TMC        Added include for Moofwars.h so that MrC will compile
  16. 8/15/96        TMC     Initial Release
  17.  
  18. Copyright © 1996, 1997 Apple Computer, Inc., All Rights Reserved
  19.  
  20.  
  21. You may incorporate this sample code into your applications without
  22. restriction, though the sample code has been provided "AS IS" and the
  23. responsibility for its operation is 100% yours.  However, what you are
  24. not permitted to do is to redistribute the source as "DSC Sample Code"
  25. after having made changes. If you're going to re-distribute the source,
  26. we require that you make it clear in the source that the code was
  27. descended from Apple Sample Code, but that you've made changes.
  28.  
  29. *************************************************************************************/
  30.  
  31. #include "Moofwars.h"
  32. #include "TShipSprite.h"
  33. #include "TShotSprite.h"
  34.  
  35. TShipSprite::TShipSprite (TShipSpriteData *data):
  36. TSprite((TSpriteData *) data)
  37. {
  38.     fRotateInterval = data->rotateInterval;
  39.     fRotateValue = 0;
  40.     fShotInterval = data->shotInterval;
  41.     fShotValue = 0;
  42.     fDirection = fFace;
  43.     fShotsGroup = data->shotsGroup;
  44. }
  45.  
  46.  
  47. TShipSprite::~TShipSprite (void)
  48. {
  49. }
  50.     
  51. void
  52. TShipSprite::ProcessSprite (void)
  53. {        
  54.     
  55.     // Fire a shot
  56.     if (CheckKey ((unsigned char *) &gCommandKeys, kShipFireShot) && fShotValue == 0)
  57.     {
  58.         TShotSpriteData        shotData;
  59.         TShotSprite            *shot;
  60.  
  61. #if qUsingSound
  62.         if (gPlayingSound)
  63.             HY_PlaySoundHandle (3, gSounds[4], NULL, 0, true);
  64. #endif        
  65.         // Set the firing delay so that we don't necessarily shoot every frame.
  66.         fShotValue = fShotInterval;
  67.         
  68.         shotData.spriteData.spriteType = TShotSprite::kSpriteType;
  69.         shotData.spriteData.visibility = kVisible;
  70.         shotData.spriteData.face = 0;
  71.         shotData.spriteData.collectionID = 1001;
  72.         shotData.spriteData.preloadedCollection = NULL;
  73.         shotData.duration = 30;
  74.  
  75.         shotData.spriteData.initialX = fCoordX - 80*gCosLookup [fDirection];
  76.         shotData.spriteData.initialY = fCoordY - 80*gSinLookup [fDirection];
  77.         shotData.spriteData.initialXVelocity = fVelocityX - 30*gCosLookup[fDirection];
  78.         shotData.spriteData.initialYVelocity = fVelocityY - 30*gSinLookup [fDirection];
  79.             
  80.         shot = new TShotSprite (&shotData);
  81.         shot->AddToGroup (fShotsGroup);
  82.         
  83.     }
  84.     
  85.     // Fire a cluster
  86.     if (CheckKey ((unsigned char *) &gCommandKeys, kShipFireCluster) && fShotValue == 0)
  87.     {
  88.         TShotSpriteData     shotData;
  89.         TShotSprite            *shot;
  90. #if qUsingSound        
  91.         if (gPlayingSound)
  92.             HY_PlaySoundHandle (3, gSounds[4], NULL, 0, true);
  93. #endif
  94.         // Set the firing delay so that we don't necessarily shoot every frame.
  95.         fShotValue = fShotInterval;
  96.  
  97.         shotData.spriteData.spriteType = TShotSprite::kSpriteType;
  98.         shotData.spriteData.visibility = kVisible;
  99.         shotData.spriteData.face = 0;
  100.         shotData.spriteData.collectionID = 1001;
  101.         shotData.spriteData.preloadedCollection = NULL;
  102.         shotData.duration = 30;
  103.  
  104.         for (int loop = 0; loop < 48; loop++) // one shot in every direction
  105.         {
  106.             shotData.spriteData.initialX = fCoordX - 80*gCosLookup[loop];
  107.             shotData.spriteData.initialY = fCoordY - 80*gSinLookup[loop];
  108.             shotData.spriteData.initialXVelocity = fVelocityX - 30*gCosLookup[loop];
  109.             shotData.spriteData.initialYVelocity = fVelocityY - 30*gSinLookup[loop];
  110.             
  111.             shot = new TShotSprite (&shotData);
  112.             shot->AddToGroup (fShotsGroup);
  113.         }
  114.         
  115.     }    
  116.     
  117.     // Rotate right
  118.     if (CheckKey ((unsigned char *) &gCommandKeys, kShipRotateRight) && fRotateValue == 0)
  119.     {
  120.         fDirection = (fDirection+1) % 48;
  121.         fRotateValue = fRotateInterval;
  122.     }
  123.     
  124.     // Rotate left
  125.     if (CheckKey ((unsigned char *) &gCommandKeys, kShipRotateLeft) && fRotateValue == 0)
  126.     {
  127.         fDirection = (fDirection+47) % 48;
  128.         fRotateValue = fRotateInterval;
  129.     }
  130.     
  131.     // Thrust.  The fCurrentFace is used so that we can provide different frames
  132.     // if we have thrust active.
  133.     
  134.     if (CheckKey ((unsigned char *) &gCommandKeys, kShipApplyThrust) )
  135.     {
  136.         fVelocityY -= gSinLookup[fDirection] >> 1;
  137.         fVelocityX -= gCosLookup[fDirection] >> 1;
  138.         fFace = fDirection;
  139.     }
  140.     else
  141.         fFace = fDirection;
  142.     
  143.     // Adjust our intervals before we can turn/shoot again.
  144.     if (fRotateValue > 0)
  145.         fRotateValue--;
  146.         
  147.     if (fShotValue > 0)
  148.         fShotValue--;
  149.     
  150.     // Process our ship's movement
  151.     TSprite::ProcessSprite();
  152.     TSprite::Bounce (&gWorldBounds);
  153. }
  154.  
  155.  
  156. void
  157. TShipSprite::Collision (TSprite *theSprite)
  158. {
  159. #if qUsingSound
  160.     if (gPlayingSound)
  161.         HY_PlaySoundHandle (4, gSounds[5], NULL, 0, false);
  162. #endif
  163. #pragma unused (theSprite)
  164. }